// @File  : user.go
// @Author: JunLong.Liao&此处不应有BUG!
// @Date  : 2021/5/28
// @slogan: 又是不想写代码的一天，神兽保佑，代码无BUG！
//         ┏┓      ┏┓
//        ┏┛┻━━━━━━┛┻┓
//        ┃     ღ    ┃
//        ┃  ┳┛   ┗┳ ┃
//        ┃     ┻    ┃
//        ┗━┓      ┏━┛
//          ┃      ┗━━━┓
//          ┃ 神兽咆哮!  ┣┓
//          ┃         ┏┛
//          ┗┓┓┏━━━┳┓┏┛
//           ┃┫┫   ┃┫┫
//           ┗┻┛   ┗┻┛

package model

import "github.com/jinzhu/gorm"

type TUser struct {
	ID          int64  `gorm:"column:id" json:"id" form:"id"`
	Username    string `gorm:"column:username" json:"username" form:"username"`
	Password    string `gorm:"column:password" json:"password" form:"password"`
	CreatedTime int64  `gorm:"column:created_time" json:"created_time" form:"created_time"`
}

func (h *TUser) TableName() string {
	return "t_user"
}

func (h *TUser) CreateUer(DB *gorm.DB) error {
	// INSERT INTO `t_user` (`username`,`password`,`created_time`) VALUES ('test','e10adc3949ba59abbe56e057f20f883e',1622191367)
	// INSERT INTO `test3`.`t_user`(`id`, `username`, `password`, `created_time`) VALUES (NULL, 'admin2', 'e10adc3949ba59abbe56e057f20f883e', 1622189616)
	return DB.Table(h.TableName()).Create(&h).Error
}
func (h *TUser) GetUserByUsername(DB *gorm.DB) error {
	// list := make([]*TUser,0)
	// DB.Table(h.TableName()).Where("username = ?",h.Username).Find(&list).Error
	// SELECT * FROM `t_user`  WHERE (username = 'test') ORDER BY `t_user`.`id` ASC LIMIT 1
	return DB.Table(h.TableName()).Where("username = ?", h.Username).First(&h).Error
}

func (h *TUser) UpdateUser(DB *gorm.DB, ID string) error {
	// UPDATE `test3`.`t_user` SET `created_time` = 1622189612 WHERE `id` = 1
	return DB.Table(h.TableName()).Where("id = ?", ID).Updates(&h).Error
}

func (h *TUser) DeleteUser(DB *gorm.DB, ID string) error {
	// DELETE FROM `test3`.`t_user` WHERE `id` = 3
	return DB.Table(h.TableName()).Where("id = ?", ID).Delete(&h).Error
}
func (h *TUser) GetUserList(DB *gorm.DB) (list []*TUser, err error) {
	list = make([]*TUser, 0)

	if err := DB.Table(h.TableName()).Where("id > ?", 0).Find(&list).Error; err != nil {
		return nil, err
	}

	return
}
